<template>
  <el-drawer v-model="visible" :title="form.id ? '编辑' : '新增'" size="50%">
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" label-width="auto" v-loading="loading">
      <el-row>
        <el-col :span="12" class="mb20">
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" maxlength="20" placeholder="请输入名称"/>
          </el-form-item>
          <el-form-item prop="sortOrder">
            <template #label>分片值
              <tip content="分片值取决于模型自身能力,理论上分片值越大越准确"/>
            </template>
            <el-input-number v-model="form.fragmentSize" :min="2000" :max="9999" :step="1"/>
          </el-form-item>
          <el-form-item prop="sortOrder">
            <template #label>排序值
              <tip content="越大展示越靠前"/>
            </template>
            <el-input-number v-model="form.sortOrder" :min="1" :max="9999" :step="1"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="封面" prop="avatarUrl">
            <upload-img v-model:image-url="form.avatarUrl" borderRadius="50%"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24" class="mb20">
          <el-form-item label="欢迎语" prop="welcomeMsg">
            <el-input
                v-model="form.welcomeMsg"
                type="textarea"
                placeholder="描述知识库的内容，详尽的描述将帮助AI能深入理解该知识库的内容，能更准确的检索到内容，提高该知识库的命中率。"
                maxlength="1024"
                show-word-limit
                rows="6"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-collapse>
        <el-collapse-item title="高级配置" name="1">
          <el-row :gutter="24">
            <el-col :span="12" class="mb20">
              <el-form-item label="多轮会话" prop="multiRound">
                <template #label>会话轮数
                  <tip content="会话轮数，0代表不记忆上文会话"/>
                </template>
                <el-input-number v-model="form.multiRound" :min="0" :max="5" :step="1"/>
              </el-form-item>
            </el-col>

            <el-col :span="12" class="mb20">
              <el-form-item prop="score">
                <template #label>匹配率
                  <tip content="向量数据库匹配率，建议不低于 50%"/>
                </template>
                <el-slider v-model="form.score" :step="10" :min="10" :max="90" :format-tooltip="value => value + '%'"
                           show-stops/>
              </el-form-item>
            </el-col>

            <el-col :span="12" class="mb20">
              <el-form-item prop="topK">
                <template #label>匹配条数
                  <tip content="向量数据库匹配最多几条结果"/>
                </template>
                <el-input-number v-model="form.topK" :min="1" :max="5" :step="1"/>
              </el-form-item>
            </el-col>


            <el-col :span="12" class="mb20">
              <el-form-item prop="emptyDesc">
                <template #label>空提示
                  <tip content="未匹配的时候，返回的文案"/>
                </template>
                <el-input v-model="form.emptyDesc" placeholder="请输入描述"/>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="8" class="mb20">
              <el-form-item label="文档总结" prop="preSummary">
                <el-switch v-model="form.preSummary" :active-value="'1'"
                           :inactive-value="'0'"/>
              </el-form-item>
            </el-col>
            <el-col :span="8" class="mb20">
              <el-form-item label="会话压缩" prop="preCompress">
                <el-switch v-model="form.preCompress" :active-value="'1'"
                           :inactive-value="'0'"/>
              </el-form-item>
            </el-col>
            <el-col :span="8" class="mb20">
              <el-form-item prop="standardFlag">
                <template #label>标注数据
                  <tip content="使用已经标注修正后的答案，直接返回"/>
                </template>
                <el-switch v-model="form.standardFlag" :active-value="'1'"
                           :inactive-value="'0'"/>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="24" class="mb20">
              <el-form-item label="底部信息" prop="footer">
                <el-input
                    v-model="form.footer"
                    type="textarea"
                    placeholder="聊天框底部的信息，支持 HTML 语法"
                    maxlength="1024"
                    show-word-limit
                    rows="6"
                />
              </el-form-item>
            </el-col>
          </el-row>

        </el-collapse-item>

        <el-collapse-item title="安全配置">
          <el-row :gutter="24">
            <el-col :span="12" class="mb20">
              <el-form-item label="是否对外" prop="publicFlag">
                <el-switch v-model="form.publicFlag" :active-value="'1'"
                           :inactive-value="'0'"/>
              </el-form-item>
            </el-col>
            <el-col :span="12" class="mb20">
              <el-form-item prop="publicPassword">
                <template #label>安全密钥
                  <tip content="对外服务，需要用户输入的密码"/>
                </template>
                <el-input v-model="form.publicPassword" placeholder="请输入密码"/>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="12" class="mb20">
              <el-form-item label="敏感词过滤" prop="sensitiveFlag">
                <el-switch v-model="form.sensitiveFlag" :active-value="'1'"
                           :inactive-value="'0'"/>
              </el-form-item>
            </el-col>
            <el-col :span="12" class="mb20">
              <el-form-item prop="sensitiveMsg">
                <template #label>提示
                  <tip content="命中敏感词，返回的文案"/>
                </template>
                <el-input v-model="form.sensitiveMsg" placeholder="请输入描述"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-collapse-item>
      </el-collapse>

    </el-form>
    <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
        </span>
    </template>

  </el-drawer>
</template>

<script setup lang="ts" name="AiDatasetDialog">
import {useDict} from '/@/hooks/dict';
import {useMessage} from "/@/hooks/message";
import {getDetails, addObj, putObj, validateName} from '/@/api/knowledge/aiDataset'
import {rule} from '/@/utils/validate';
import UploadImg from "/@/components/Upload/Image.vue";

const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)
// 定义字典
const {yes_no_type} = useDict('yes_no_type')

// 提交表单数据
const form = reactive({
  id: '',
  name: '',
  avatarUrl: '',
  description: '',
  units: '0',
  fileSize: '0',
  multiRound: 5,
  topK: 3,
  score: 30,
  fragmentSize: 2000,
  sortOrder: 1,
  emptyDesc: '知识库未匹配相关问题，请重新提问',
  sensitiveFlag: '1',
  preSummary: '1',
  preCompress: '0',
  sensitiveMsg: '您输入内容包含敏感词，请重新输入',
  publicFlag: '1',
  publicPassword: '' || undefined,
  standardFlag: '0',
  welcomeMsg: '',
  footer: '',
});

// 定义校验规则
const dataRules = ref({
  name: [{required: true, message: '知识库名称不能为空', trigger: 'blur'}, {
    validator: (rule: any, value: any, callback: any) => {
      validateName(rule, value, callback, form.id !== '');
    },
    trigger: 'blur',
  }],
  welcomeMsg: [{required: true, message: '欢迎语不能为空', trigger: 'blur'}],
  multiRound: [{required: true, message: '多轮会话不能为空', trigger: 'blur'}],
  topK: [{required: true, message: '多轮会话不能为空', trigger: 'blur'}],
  avatarUrl: [{required: true, message: '封面不能为空', trigger: 'blur'}],
  emptyDesc: [{validator: rule.overLength, trigger: 'blur'}, {
    required: true,
    message: '提示不能为空',
    trigger: 'blur'
  }],
  sensitiveMsg: [{validator: rule.overLength, trigger: 'blur'}, {
    required: true,
    message: '提示不能为空',
    trigger: 'blur'
  }],
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.id = ''

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取aiDataset信息
  if (id) {
    form.id = id
    getaiDatasetData(id)
  }
};

// 提交
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => {
  });
  if (!valid) return false;

  if (form.id && form.publicPassword?.includes('**')) {
    form.publicPassword = undefined
  }

  try {
    loading.value = true;
    form.id ? await putObj(form) : await addObj(form);
    useMessage().success(form.id ? '修改成功' : '添加成功');
    visible.value = false;
    emit('refresh');
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};


// 初始化表单数据
const getaiDatasetData = (id: string) => {
  // 获取数据
  loading.value = true
  getDetails({id}).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};

// 暴露变量
defineExpose({
  openDialog
});
</script>
